package com.ssh.admin.action;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh.common.log.LogFactory;
import com.ssh.common.log.LogName;
import com.ssh.common.util.JsonResult;
import com.ssh.common.util.Page;
import com.ssh.entity.Brand;
import com.ssh.entity.SuperMarket;
import com.ssh.entity.Task;
import com.ssh.entity.TaskImage;
import com.ssh.manager.TaskManager;

@Controller
@ParentPackage("admin")
@Scope("prototype")
@Results(value={
@Result(name="success", location="/admin/task/taskList.jsp"),
@Result(name="taskImg", location="/admin/task/taskImage.jsp"),
@Result(name="findTaskByPage", type="json", params={"root","page","excludeproperties","page\\.curPageNum,page\\.totalPageNum,page\\.totalCount," +
		"page\\.numberPerPage,page\\.pageData\\[\\d+\\]\\.id,page\\.pageData\\[\\d+\\]\\.name,page\\.pageData\\[\\d+\\]\\.content," +
		"page\\.pageData\\[\\d+\\]\\.createDate,page\\.pageData\\[\\d+\\]\\.startDate,page\\.pageData\\[\\d+\\]\\.endDate"}),
@Result(name="findList", type="json", params={"root","page","excludeproperties","page\\.curPageNum,page\\.totalPageNum,page\\.totalCount," +
		"page\\.numberPerPage,page\\.pageData\\[\\d+\\]\\.id,page\\.pageData\\[\\d+\\]\\.url,page\\.pageData\\[\\d+\\]\\.imgType,page\\.pageData\\[\\d+\\]\\.createDate," +
		"page\\.pageData\\[\\d+\\]\\.taskBrand\\.task\\.id,page\\.pageData\\[\\d+\\]\\.taskBrand\\.task\\.content,page\\.pageData\\[\\d+\\]\\.superMarket\\.id," +
		"page\\.pageData\\[\\d+\\]\\.superMarket\\.name,page\\.pageData\\[\\d+\\]\\.taskBrand\\.brand\\.name,page\\.pageData\\[\\d+\\]\\.taskBrand\\.brand\\.id"}),
@Result(name="findAll", type="json", params={"root","tasks","excludeproperties","tasks\\[\\d+\\]\\.id,tasks\\[\\d+\\]\\.content"}),
@Result(name="updateTask", type="json", params={"root","jr","excludeproperties","jr\\.code"}),
@Result(name="deleteTask", type="json", params={"root","page","excludeproperties","page\\.curPageNum"}),
@Result(name="deleteImg", type="json", params={"root","page","excludeproperties","page\\.curPageNum"})
})
public class TaskAction extends ActionSupport{
	private static final long serialVersionUID = -268757009268660942L;
	@Resource(name="taskManagerImpl")
	private TaskManager taskManager;
	private JsonResult jr=new JsonResult();
	private Page page = new Page();
	private String isDesc;
	private Task task;
	private Brand brand;
	private SuperMarket superMarket;
	private TaskImage taskImage;
	private List<Task> tasks;
	private String ids;
	
	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public List<Task> getTasks() {
		return tasks;
	}

	public void setTasks(List<Task> tasks) {
		this.tasks = tasks;
	}

	public String goTaskImgJsp(){
		return "taskImg";
	}
	
	public String findAll(){
		tasks = taskManager.getAll();
		return "findAll";
	}
	
	public String findList(){
		Map conditionMap = new HashMap();
		conditionMap.put("task", task.getContent());
		conditionMap.put("brand", brand.getId());
		conditionMap.put("market", superMarket.getName());
		taskManager.findImgList(conditionMap, page, isDesc);
		return "findList";
	}
	
	public String deleteImg(){
		try {
			if(ids!=null && !ids.equals("")){
				String[] d = ids.split(",");
				for (int i = 0; i < d.length; i++) {
					taskManager.deleteImg(new Long(d[i]));
				}
			}else{
				taskManager.deleteImg(taskImage.getId());
			}
			
			int _p = (int) page.getCurPageNum();
			page.setCurPageNum(1);
			Map conditionMap = new HashMap();
			conditionMap.put("task", task.getContent());
			conditionMap.put("brand", brand.getId());
			conditionMap.put("market", superMarket.getName());
			taskManager.findImgList(conditionMap, page, isDesc);
			if(_p<=page.getTotalPageNum()){
				page.setCurPageNum(_p);
			}else{
				page.setCurPageNum(_p-1);
			}
			jr.setCode("y");
		} catch (Exception e) {
			jr.setCode("n");
			LogFactory.getDefaultLog().error("任务图片删除失败", LogName.API);
			e.printStackTrace();
		}
		return "deleteImg";
	}

	/**
	 * 分页查询系统
	 * @return
	 * @throws Exception
	 */
	public String findTaskByPage() throws Exception{
		//page.setNumberPerPage(20);
		taskManager.findTaskByPage(page, isDesc);
		return "findTaskByPage";
	}
	
	
	public String saveTask(){
		task.setDel(0);
		task.setCreateDate(new Timestamp(System.currentTimeMillis()));
		task=taskManager.saveOrUpdateTask(task);
		return "success";	
	}
	public String updateTask(){
		Task t = taskManager.fingTaskById(task.getId());
		t.setContent(task.getContent());
		t.setStartDate(task.getStartDate());
		t.setEndDate(task.getEndDate());
		taskManager.saveOrUpdateTask(t);
		jr.setCode("0");
		return "updateTask";	
	}
	
	public String deleteTask(){
		if(ids!=null && !ids.equals("")){
			String[] d = ids.split(",");
			for (int i = 0; i < d.length; i++) {
				taskManager.deleteTask(new Long(d[i]));
			}
		}else{
			taskManager.deleteTask(task.getId());
		}
		
		int _p = (int) page.getCurPageNum();
		page.setNumberPerPage(20);
		page.setCurPageNum(1);
		taskManager.findTaskByPage(page, isDesc);
		if(_p<=page.getTotalPageNum()){
			page.setCurPageNum(_p);
		}else{
			page.setCurPageNum(_p-1);
		}
		return "deleteTask";	
	}
	
	
	public String goTaskList(){
		
		return "success";
	}
	public JsonResult getJr() {
		return jr;
	}

	public void setJr(JsonResult jr) {
		this.jr = jr;
	}

	public String getIsDesc() {
		return isDesc;
	}

	public void setIsDesc(String isDesc) {
		this.isDesc = isDesc;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}
	public Task getTask() {
		return task;
	}

	public TaskManager getTaskManager() {
		return taskManager;
	}

	public void setTaskManager(TaskManager taskManager) {
		this.taskManager = taskManager;
	}

	public Brand getBrand() {
		return brand;
	}

	public void setBrand(Brand brand) {
		this.brand = brand;
	}

	public SuperMarket getSuperMarket() {
		return superMarket;
	}

	public void setSuperMarket(SuperMarket superMarket) {
		this.superMarket = superMarket;
	}

	public void setTask(Task task) {
		this.task = task;
	}

	public TaskImage getTaskImage() {
		return taskImage;
	}

	public void setTaskImage(TaskImage taskImage) {
		this.taskImage = taskImage;
	}
}
